<template>
  <div id="home">
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <tab-control ref="tabControl1" v-show="isTabFixed" class="tab-control" :titles="['流行', '新款', '精选']" @tabClick="tabClick"/>

    <scroll class="content" ref="scroll" :probe-type="3" @scroll="contentScroll" :pull-up-load="true" @pullingUp="loadMore">
      <home-swiper :banners="banners" @swiperImgLoad="swiperImgLoad"/>
      <recommend-view :recommends="recommends"/>
      <feature-view/>
      <tab-control ref="tabControl2" :titles="['流行', '新款', '精选']" @tabClick="tabClick"/>
      <goods-list :goods="showGoods"/>

      <ul>
        <li>商品列表1</li>
        <li>商品列表2</li>
        <li>商品列表3</li>
        <li>商品列表4</li>
        <li>商品列表5</li>
        <li>商品列表6</li>
        <li>商品列表7</li>
        <li>商品列表8</li>
        <li>商品列表9</li>
        <li>商品列表10</li>
        <li>商品列表11</li>
        <li>商品列表12</li>
        <li>商品列表13</li>
        <li>商品列表14</li>
        <li>商品列表15</li>
        <li>商品列表16</li>
        <li>商品列表17</li>
        <li>商品列表18</li>
        <li>商品列表19</li>
        <li>商品列表20</li>
        <li>商品列表21</li>
        <li>商品列表22</li>
        <li>商品列表23</li>
        <li>商品列表24</li>
        <li>商品列表25</li>
        <li>商品列表26</li>
        <li>商品列表27</li>
        <li>商品列表28</li>
        <li>商品列表29</li>
        <li>商品列表30</li>
        <li>商品列表31</li>
        <li>商品列表32</li>
        <li>商品列表33</li>
        <li>商品列表34</li>
        <li>商品列表35</li>
        <li>商品列表36</li>
        <li>商品列表37</li>
        <li>商品列表38</li>
        <li>商品列表39</li>
        <li>商品列表40</li>
        <li>商品列表41</li>
        <li>商品列表42</li>
        <li>商品列表43</li>
        <li>商品列表44</li>
        <li>商品列表45</li>
        <li>商品列表46</li>
        <li>商品列表47</li>
        <li>商品列表48</li>
        <li>商品列表49</li>
        <li>商品列表50</li>
      </ul>
    </scroll>

    <back-top @click.native="backTop" v-show="isShowBackTop"/>
  </div>
</template>

<script>
  import NavBar from 'components/common/navbar/NavBar'
  import TabControl from 'components/content/tabControl/TabControl'
  import GoodsList from 'components/content/goods/GoodsList'
  import Scroll from 'components/common/scroll/Scroll'

  import HomeSwiper from './childComponents/HomeSwiper'
  import RecommendView from './childComponents/RecommendView'
  import FeatureView from './childComponents/FeatureView'

  import { getHomeMultidata, getHomeGoods } from 'network/home'
  import { itemListenerMixin, backTopMixin } from 'common/mixin'

  export default {
    name: 'Home',
    components: {
      NavBar,
      TabControl,
      GoodsList,
      Scroll,
      HomeSwiper,
      RecommendView,
      FeatureView,
    },
    mixins: [itemListenerMixin, backTopMixin],
    data() {
      return {
        // result: null,
        banners: [],
        recommends: [],
        goods: {
          'pop': {page: 0, list: []},
          'new': {page: 0, list: []},
          'sell': {page: 0, list: []}
        },
        currentType: 'pop',
        // isShowBackTop: false,
        tabOffsetTop: Number,
        isTabFixed: false,
        saveY: 0,
        // itemImgListener: null
      }
    },
    computed: {
      showGoods() {
        return this.goods[this.currentType].list;
      }
    },
    activated() {
      this.$refs.scroll.scrollTo(0, this.saveY, 0)
      this.$refs.scroll.refresh()
    },
    deactivated() {
      this.saveY = this.$refs.scroll.getScrollY()

      // 取消全局事件的监听
      this.$bus.$off('itemImgLoad', this.itemImgListener)
    },
    created() {
      // 1.请求多个数据
      this.getHomeMultidata()
      // 2.请求商品数据
      this.getHomeGoods('pop')
      this.getHomeGoods('new')
      this.getHomeGoods('sell')
    },
    mounted() {
      // 监听item中图片是否加载完成
      // const refresh = debounce(this.$refs.scroll.refresh, 100)

      // 对监听的事件进行保存
      // this.itemImgListener = () => {
      //   refresh()
      // }

      // 事件总线监听GoodsListItem
      // this.$bus.$on('itemImgLoad', this.itemImgListener)
      // this.$bus.$on('itemImgLoad', () => {
      //   refresh()
      // })
    },
    methods: {
      /**
       * 事件监听相关的方法
       */
      tabClick(index) {
        switch(index) {
          case 0:
            this.currentType = 'pop'
            break
          case 1:
            this.currentType = 'new'
            break
          case 2:
            this.currentType = 'sell'
            break
        }
        this.$refs.tabControl1.currentIndex = index;
        this.$refs.tabControl2.currentIndex = index;
      },
      // backTop() {
      //   this.$refs.scroll.scrollTo(0, 0);
      // },
      contentScroll(position) {
        // 1.判断BackTop是否显示
        // this.isShowBackTop = -position.y > 600;
        this.listenShowBackTop(position); // 混入 common/mixin.js

        // 2.决定tabControl是否吸顶(position: fixed)
        this.isTabFixed = -position.y > this.tabOffsetTop;
      },
      loadMore() {
        this.getHomeGoods(this.currentType);
        // this.$refs.scroll.scroll.refresh();
      },
      swiperImgLoad() {
        // 获取tabControl的offsetTop
        // 所有的组件都有一个属性 $el: 用于获取组件中的元素
        this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
      },
      /**
       * 网络请求相关的方法
       */
      getHomeMultidata() {
        getHomeMultidata().then(res => {
          // console.log(res);
          // this.result = res;
          this.banners = res.data.banner.list;
          this.recommends = res.data.recommend.list;
        })
      },
      getHomeGoods() {
        const page = this.goods[type].page + 1;
        getHomeGoods(type, page).then(res => {
          this.goods[type].list.push(...res.data.list);
          this.goods[type].page += 1;
          // 完成上拉加载更多
          this.$refs.scroll.finishPullUp();
        })
      }
    }
  }
</script>

<style scoped>
  /* #home {
    position: relative;
    height: 100vh;
    padding-top: 44px;
  } */

  .home-nav {
    /* position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 9; */
    background-color: #ff8198;
    color: #fff;
  }

  /* .tab-control {
    position: sticky;
    top: 44px;
    z-index: 9;
  } */

  .tab-control {
    position: relative;
    z-index: 9;
    background-color: #fff;
  }

  /* .fixed {
    position: fixed;
    left: 0;
    right: 0;
    top: 44px;
  } */

  .content {
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
    overflow: hidden;
  }

  /* .content {
    height: calc(100% - 93px);
    margin-top: 44px;
    overflow: hidden;
  } */
</style>
